﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AutorizacionViajesMVC.WSRendir;
using System.ServiceModel;

namespace AutorizacionViajesMVC.Controllers
{
    public class RendirController : Controller
    {
        public List<WSRendir.Rendir> listarRendir()
        {
            WSRendir.RendirsClient proxy = new WSRendir.RendirsClient();
            List<WSRendir.Rendir> rendir = proxy.ListarRendir().ToList();
            return rendir;
        }

        public WSRendir.Rendir obtenerRendir(int Cod)
        {
            WSRendir.RendirsClient proxy = new WSRendir.RendirsClient();
            WSRendir.Rendir rendir = proxy.ObtenerRendir(Cod);
            return rendir;
        }

        public WSRendir.Rendir crearRendir(int codau, int codemp, string estasol, double hot, double via, double movi, double tele, double gast, double iaere, double pasa, double combu, double otro)
        {
            WSRendir.RendirsClient proxy = new WSRendir.RendirsClient();
            return proxy.CrearRendir(codau, codemp, estasol, hot, via, movi, tele, gast, iaere, pasa, combu, otro);
        }

        public ActionResult Index()
        {
            List<WSRendir.Rendir> rendir = listarRendir();
            return View(rendir);
        }


        public ActionResult Details(int id)
        {
            WSRendir.Rendir rendir = obtenerRendir(id);
            return View(rendir);
        }


        public ActionResult Create()
        {
            WSRendir.Rendir rendir = new WSRendir.Rendir();
            return View(rendir);
        } 

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                WSRendir.Rendir res = crearRendir(int.Parse(collection["CodAU"]), int.Parse(collection["CodEMP"]), collection["ESTADOSOL"], double.Parse(collection["Hotel"]), double.Parse(collection["Viaticos"]), double.Parse(collection["Movilidad"]), double.Parse(collection["Telefono"]), double.Parse(collection["Gastos"]), double.Parse(collection["IMPAereo"]), double.Parse(collection["Pasaje"]), double.Parse(collection["Combustible"]), double.Parse(collection["Otros"]));
                return RedirectToAction("Index");
            }
            catch (FaultException<ValidationException> e)
            {
                TempData["error"] = e.Message;
                return View("Create");
            }
            catch (Exception ex)
            {
                TempData["error"] = ex.Message;
                return RedirectToAction("Create");
            }
        }
        
        //
        // GET: /Rendir/Edit/5
 
        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Rendir/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Rendir/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Rendir/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
